package com.mgg.goods.service.goods.impl;

import com.github.pagehelper.PageHelper;
import com.mgg.common.dao.DaoSupport;
import com.mgg.goods.service.goods.GoodsService;
import com.mgg.goods.vo.GoodsSimpleVO;
import org.apache.commons.collections.MapUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author: lijun
 * @Date: 2018/9/19 11:09
 */
@Service("goodsService")
public class GoodsServiceImpl implements GoodsService{

    @Resource(name = "daoSupport")
    private DaoSupport dao;


    @Override
    public void saveQueryGoodsWords(String word) {
        Map<String,Object> map =  dao.findForObject("GoodsMapper.getGoodsSearchWord", word);
        if (MapUtils.isEmpty(map)) {
            dao.save("GoodsMapper.insertNewGoodsSearchWord", word);
        } else {
            dao.update("GoodsMapper.updateGoodsSearchWordNum", word);
        }
    }

    @Override
    public List<String> listGoodsSearchWordHot() {
        List<String> list = dao.findForList("GoodsMapper.listGoodsSearchWordHot", null);
        return list;
    }

    @Override
    public List<Map<String, String>> listRecommendGoods() {
        List<Map<String, String>> list = dao.findForList("GoodsMapper.listRecommendGoods", null);
        return list;
    }

    @Override
    public void setGoodsQty(GoodsSimpleVO goods) {
        Map<String,Object> qtyMap = dao.findForObject("GoodsMapper.getGoodsQtyById",goods.getGoodsId());
        if (!MapUtils.isEmpty(qtyMap)) {
            long invQty = Long.parseLong(qtyMap.get("inv_qty").toString());
            long salesQty = Long.parseLong(qtyMap.get("sales_qty").toString());
            goods.setInvQty(invQty);
            goods.setSalesQty(salesQty);
        }
    }

    @Override
    public List<String> listSeckillGoodsStatus(String goodsId) {
        List<String> statusList = dao.findForList("GoodsMapper.listSeckillGoodsStatus", goodsId);
        return statusList;
    }

    @Override
    public String getAdPicture(int type) {
        String adPic =  dao.findForObject("GoodsMapper.getAdPicture", type);
        return adPic;
    }

    @Override
    public List<GoodsSimpleVO> listRankingsGoods(Pageable page) {
        PageHelper.startPage(page.getPageNumber(), page.getPageSize());
        List<GoodsSimpleVO> goodsList = dao.findForList("GoodsMapper.listRankingsGoods",null);
        return goodsList;
    }
}
